package com.regis.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.regis.comment.uilts.ThreadDataUtil;
import com.regis.entity.db.AddressBook;
import com.regis.entity.vo.ResultVo;
import com.regis.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 计算机系 ITAEM 摆渡人
 * @description 地址接口
 * @date 2022/8/23 5:04
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private CacheManager cacheManager;

    /**
     * 新增地址
     * @param addressBook AddressBook
     * @return AddressBook
     */
    @CacheEvict(value = "addressBook",key = "#threadDataUtil.value")
    @PostMapping
    public ResultVo<AddressBook> save(@RequestBody AddressBook addressBook,ThreadDataUtil threadDataUtil) {
        addressBook.setUserId(ThreadDataUtil.getValue());
        addressBookService.saveOrUpdate(addressBook);
        return ResultVo.success(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook AddressBook
     * @return AddressBook
     */
    @CacheEvict(value = "addressBook",key = "#threadDataUtil.value")
    @PutMapping("/default")
    public ResultVo<AddressBook> setDefault(@RequestBody AddressBook addressBook,ThreadDataUtil threadDataUtil) {
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId, ThreadDataUtil.getValue());
        wrapper.set(AddressBook::getIsDefault, 0);
        //SQL:update address_book set is_default = 0 where user_id = ?
        addressBookService.update(wrapper);

        addressBook.setIsDefault(1);
        //SQL:update address_book set is_default = 1 where id = ?
        addressBookService.updateById(addressBook);
        return ResultVo.success(addressBook);
    }

    /**
     * 根据id查询地址
     * @param id id
     * @return AddressBook
     */
    @Cacheable(value = "addressBook",key = "#id",unless = "#result.data == null")
    @GetMapping("/{id}")
    public ResultVo<AddressBook> get(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return ResultVo.success(addressBook);
        } else {
            return ResultVo.error("没有找到该对象");
        }
    }

    /**
     * 查询默认地址
     * @return AddressBook
     */
    @Cacheable(value = "addressBook",key = "#threadDataUtil.value + '_1'",unless = "#result.data == null")
    @GetMapping("/default")
    public ResultVo<AddressBook> getDefault(ThreadDataUtil threadDataUtil) {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, ThreadDataUtil.getValue());
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        return ResultVo.success(addressBook);
    }

    /**
     * 查询指定用户的全部地址
     * @param addressBook AddressBook
     * @return AddressBook
     */
    @Cacheable(value = "addressBook",key = "#threadDataUtil.value",unless = "#result.data == null")
    @GetMapping("/list")
    public ResultVo<List<AddressBook>> list(AddressBook addressBook,ThreadDataUtil threadDataUtil) {
        addressBook.setUserId(ThreadDataUtil.getValue());
        List<AddressBook> list = null;


        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        list = addressBookService.list(queryWrapper);
        return ResultVo.success(list);
    }

}
